package com.cema.manage.project.android.biz;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.cema.manage.common.exception.BizException;
import com.cema.manage.project.android.TestBaseContrller;
import com.cema.manage.project.android.model.PageVo;
import com.cema.manage.project.android.model.UserDailyActivityVo;
import com.cema.manage.project.android.result.JsonResult;
import com.cema.manage.project.android.service.BizRewardRecordService;
import com.cema.manage.project.enums.RiskEnum;
import com.cema.manage.project.manage.adRecord.domain.AdRecordVo;
import com.cema.manage.project.manage.antiCheat.service.IAntiCheatService;
import com.cema.manage.project.manage.userDailyActivity.service.IUserDailyActivityService;
import com.cema.manage.project.manage.users.domain.Users;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("user_reward")
public class BizRewardRecordController extends TestBaseContrller {
    public static final Logger logger = LoggerFactory.getLogger("BizRewardRecordController");


    @Resource
    private BizRewardRecordService rewardRecordService;

    @Resource
    private IUserDailyActivityService iUserDailyActivityService;

    @Resource
    private IAntiCheatService iAntiCheatService;

    @PostMapping("sign_in")
    public JsonResult getInfo(HttpServletRequest request) {
        String realityMoney = request.getParameter("realityMoney");
        String uvChannel = request.getParameter("uvChannel");
        try {
            boolean flag = rewardRecordService.signIn(getUser(request), Integer.valueOf(request.getParameter("money")), realityMoney,uvChannel);
            if (flag) {
                return JsonResult.returnSuccess("签到成功");
            } else {
                return JsonResult.returnFail("今天已签到");
            }
        } catch (BizException e) {
            return JsonResult.returnFail(e.getMessage());
        }
    }

    @PostMapping("record")
    public JsonResult getRecord(HttpServletRequest request) {
        Users users = getUser(request);
        PageVo pageVo = (PageVo) paramUtilsInterFase.getParam(request, PageVo.class);
        return JsonResult.returnResult(JsonResult.SUCCESS, rewardRecordService.getRewardRecord(users.getId(), pageVo));
    }

    @PostMapping("trans_id")
    public JsonResult getTransId(HttpServletRequest request) {
        Users users = getUser(request);
        String transId = request.getParameter("transId");
        String oaid = request.getParameter("oaid");
        String info = request.getParameter("info");
        logger.info("////////////app配置start////////////////");
        logger.info(info);
        logger.info("////////////app配置end////////////////");
        if (StrUtil.isEmpty(oaid)) {
            return JsonResult.returnResetLogin("");
        }
        if (!users.getOaid().equals(oaid)) {
            return JsonResult.returnResetLogin("操作异常，请重新登录");
        }
        return JsonResult.returnResult(JsonResult.SUCCESS, rewardRecordService.getRewardRecordByTransId(getUser(request), transId));
    }


    @PostMapping("ad_record")
    public JsonResult createAdRecord(HttpServletRequest request, AdRecordVo recordVo) {
        iUserDailyActivityService.checkDeviceInfo(getUser(request), BeanUtil.toBean(recordVo, UserDailyActivityVo.class));
        rewardRecordService.createAdRecord(getUser(request), recordVo);
        return JsonResult.returnSuccess("创建成功");
    }

    @PostMapping("check_reward")
    public JsonResult checkReward(HttpServletRequest request) {
        Users users = getUser(request);
        //白名单
        String index = iAntiCheatService.checkRiskWhiteList(users.getId());
        if ((RiskEnum.RISK_YES.getValue().equals(index))) {
            return JsonResult.returnSuccess("成功");
        }
        index = iAntiCheatService.checkRiskAdvertisingCoins(users.getId());
        if ((RiskEnum.RISK_YES.getValue().equals(index))) {
            return JsonResult.returnFail("今日奖励已上限");
        }
        index = iAntiCheatService.checkRiskAdvertisingCoinsThanEqual(users.getId());
        if ((RiskEnum.RISK_YES.getValue().equals(index))) {
            return JsonResult.returnFail("手机环境异常");
        }
        index = iAntiCheatService.checkRiskAdvertisingCoinsThanEqualTwo(users.getId());
        if ((RiskEnum.RISK_YES.getValue().equals(index))) {
            return JsonResult.returnFail("手机环境异常");
        }
        return JsonResult.returnSuccess("成功");
    }
}
